﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
 */
import flash.filters.DropShadowFilter;

import andromeda.model.ModelCollector;
import andromeda.util.visitor.IVisitable;
import andromeda.util.visitor.IVisitor;

import asgard.display.BackgroundDisplay;

import egallery.display.UIList;
import egallery.model.ModelList;
import egallery.model.PictureModel;

import vegas.events.Delegate;

/**
 * This display contains a container to show an external picture.
 * @author eKameleon
 */
class egallery.display.gallery.PictureDisplay extends BackgroundDisplay implements IVisitable
{
	
	/**
	 * Creates a new PictureDisplay instance.
	 */
	public function PictureDisplay( target:MovieClip , nDepth:Number ) 
	{
		
		super( UIList.PICTURE, target.createEmptyMovieClip( UIList.PICTURE, nDepth ) ) ;
		
		buRRRn.eden.Application.addAuthorized( "flash.filters.DropShadowFilter" ) ;
		
		Stage.addListener(this) ;
		
		onResize = Delegate.create(this, update) ;
		
		themeColor = 0xFFFFFF ;
		themeAlpha = 100 ;
		
		container = view.createEmptyMovieClip("container", 1) ;
		container._x = margin ;
		container._y = margin ;
		
		setSize( 260 , 260 ) ;
		
		hide() ;
		
	}

	/**
	 * The container reference in the picture display.
	 */
	public var container:MovieClip ;

	/**
	 * The margin in the picture to display the container.
	 */
	public var margin:Number = 10 ;

	/**
	 * The drop shadow reference of this display.
	 */
	public var shadow:DropShadowFilter ;

	/**
	 * Accept a IVisitor object. 
	 * You can overrides this method in complexe Visitor pattern implementation.
	 */
	public function accept( visitor:IVisitor ) 
	{
		visitor.visit(this) ;
	}

	/**
	 * This method is invoqued after the draw() method in the update() method in this display.
	 */
	public function viewChanged():Void
	{
		x = ( Stage.width - width ) / 2 ;
		y = ( Stage.height - height ) / 2 ;
		background.filters = [ shadow ] ;
		container._x = margin ;
		container._y = margin ;
	}
	
	/**
	 * This callback method is invoqued when the Stage size change.
	 */
	private var onResize:Function ;

	/**
	 * Invoqued when a key is down.
	 */
	private function onKeyDown():Void
	{
 			
		var code:Number = Key.getCode() ;
		var model:PictureModel = PictureModel( ModelCollector.get( ModelList.PICTURE_MODEL ) ) ;
		try
		{
			switch (code)
			{
				case Key.RIGHT :
				{
					getLogger().info( this + " keydown Key.RIGHT") ;
					model.next() ;	
					break ;
				}
				case Key.LEFT :
				{
					getLogger().info( this + " keydown Key.LEFT") ;
					model.prev() ;	
					break ;
				}
				/*case Key.SPACE : // TEST
				{
					getLogger().info( this + " keydown Key.SPACE") ;
					model.clear() ;
					break ;	
				}
				*/
       		}
		}
		catch( error:Error )
		{
			getLogger().error( this + " keyHandler failed : " + error.toString() )  ;
		}
		
	}

	/**
	 * Invoqued when a mouse is down.
	 */
	private function onMouseDown():Void
	{
 		
 		var model:PictureModel = PictureModel( ModelCollector.get( ModelList.PICTURE_MODEL ) ) ;
 		
		var $x:Number = _root._xmouse ;
		var $w:Number = Stage.width ;
		
		try
		{
			if ( $x > ($w / 2) )
			{
				getLogger().info( this + " keydown Key.RIGHT") ;
				model.next() ;	
			}
			else
			{
				getLogger().info( this + " keydown Key.LEFT") ;
				model.prev() ;	
				break ;
			}
		}	
		catch( error:Error )
		{
			getLogger().error( this + " keyHandler failed : " + error.toString() )  ;
		}
	}

}